package com.atguigu.study.base.block;

import java.util.concurrent.*;

/**
 * @Author 快点干早点散
 * @Create 2025/4/25 22:08
 * Description:
 */
public class BlockingQueueDemo {
    public static void main(String[] args) throws InterruptedException {
//
        BlockingQueue<String> blockingQueue = new ArrayBlockingQueue<>(3);
    //当阻塞队列满时,在进行add插入 会抛异常IllegalStateException:Queue full
//        System.out.println(blockingQueue.add("a"));
//        System.out.println(blockingQueue.add("b"));
//        System.out.println(blockingQueue.add("c"));
//        System.out.println(blockingQueue.element());
        //当阻塞队列空时，再往队列里remove移除元素会抛NoSuchElementException
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());
//        System.out.println(blockingQueue.remove());

//        LinkedBlockingQueue<Object> objects = new LinkedBlockingQueue<>(3);
        //插入大于最大值输入false
//        System.out.println(objects.offer("a"));
//        System.out.println(objects.offer("b"));
//        System.out.println(objects.offer("c"));
//        System.out.println(objects.offer("d"));
            //返回移除最大值输入null
//        System.out.println(objects.poll());
//        System.out.println(objects.poll());
//        System.out.println(objects.poll());
//        System.out.println(objects.poll());
//        System.out.println(objects.peek());//检查
            //当阻塞队列满时超过最大会put 元素 阻塞put数据or响应中断退出
//       blockingQueue.put("a");
//       blockingQueue.put("b");
//       blockingQueue.put("c");
//       blockingQueue.put("c");
//       System.out.println("1");
        //当阻塞队列为空时, tacke 元素 队列会一直阻塞消费者线程直到队列可用
//       System.out.println(blockingQueue.take());
//       System.out.println(blockingQueue.take());
//       System.out.println(blockingQueue.take());
//       System.out.println(blockingQueue.take());
        System.out.println(blockingQueue.offer("a"));
        System.out.println(blockingQueue.offer("b"));
        System.out.println(blockingQueue.offer("c"));
        System.out.println(blockingQueue.offer("c",2,TimeUnit.SECONDS));
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        System.out.println(blockingQueue.poll());
        // 按照时间进行判断 超过回退出
        System.out.println(blockingQueue.poll(2,TimeUnit.SECONDS));


    }
}
